package com.zrs.gmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrs.gmall.product.mapper.BaseAttrInfoMapper;
import com.zrs.gmall.product.mapper.BaseAttrValueMapper;
import com.zrs.gmall.product.model.BaseAttrInfo;
import com.zrs.gmall.product.model.BaseAttrValue;
import com.zrs.gmall.product.service.BaseAttrInfoService;
import com.zrs.gmall.product.service.BaseAttrValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BaseAttrInfoServiceImpl extends ServiceImpl<BaseAttrInfoMapper, BaseAttrInfo> implements BaseAttrInfoService {

    @Autowired
    private BaseAttrInfoMapper baseAttrInfoMapper;

    @Autowired
    private BaseAttrValueService baseAttrValueService; //功能较多

    @Autowired
    private BaseAttrValueMapper baseAttrValueMapper;

    /**
     * 根据一,二,三级分类ID查询平台属性以及平台属性值
     *
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseAttrInfo> getAttrInfoList(Long category1Id, Long category2Id, Long category3Id) {
        //方式二:先查询平台属性名称集合
        //遍历平台属性名称 分别 查询每个平台属性包含属性值 手动set设值


        //方式一:调用持久层方法实现平台属性以及值获取-自定义SQL编写(一对多)
        //this   ServiceImpl
        return this.getBaseMapper().getAttrInfoList(category1Id, category2Id, category3Id);

    }

    /**
     * 保存/修改 平台属性以及平台属性值
     *
     * @param baseAttrInfo
     * @return
     *
     * //1.业务校验-查询该分类下是否已有当前平台属性名称
     * //2.保存平台属性记录-保存后MyBatisPlus会给实体类中主键赋值
     * //3.批量保存平台属性值
     *         //3.1 先根据平台属性值列表 将"旧数据"删除掉
     *         //3.2 按最新提交的平台属性值为准-进行批量保存
     *
     */
    //因为下面类是两个表，不能出现脏数据，需同生共死
    @Transactional(rollbackFor = Exception.class) //    如果有异常会回滚，如果当前代码块中出现了非运行时异常，则照样回滚！
    @Override
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {


        if (baseAttrInfo.getId() == null) {
            //1.业务校验-查询该分类下是否已有当前平台属性名称
            LambdaQueryWrapper<BaseAttrInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BaseAttrInfo::getAttrName, baseAttrInfo.getAttrName());
            queryWrapper.eq(BaseAttrInfo::getCategoryId, baseAttrInfo.getCategoryId());
            queryWrapper.eq(BaseAttrInfo::getCategoryLevel, baseAttrInfo.getCategoryLevel());

            //this IService
            int count = this.count(queryWrapper);

//            System.out.println("count = " + count);

            if (count > 0) {
                throw new RuntimeException("该分类下已有该平台属性!");
            }

            //2.保存平台属性记录-保存后MyBatisPlus会给实体类中主键赋值
            this.save(baseAttrInfo);
        } else {
            //修改 id不为空，说明是修改
            this.updateById(baseAttrInfo);
        }

        //3.批量保存平台属性值
        //3.1 先根据平台属性值列表 将"旧数据"删除掉
        LambdaQueryWrapper<BaseAttrValue> attrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        attrValueLambdaQueryWrapper.eq(BaseAttrValue::getAttrId, baseAttrInfo.getId());
        List<BaseAttrValue> list = baseAttrValueService.list(attrValueLambdaQueryWrapper);
        if (!CollectionUtils.isEmpty(list)) {
            List<Long> attrValIds = list.stream().map(BaseAttrValue::getId).collect(Collectors.toList());
            baseAttrValueService.removeByIds(attrValIds);
        }

        //3.2 按最新提交的平台属性值为准-进行批量保存
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
        if (!CollectionUtils.isEmpty(attrValueList)) {
            attrValueList.stream().forEach(attVal -> {
                //将平台属性值关联到平台属性
                attVal.setAttrId(baseAttrInfo.getId());
            });
            baseAttrValueService.saveBatch(attrValueList);
        }
    }


    /**
     * 根据平台属性ID查询平台属性值集合
     * @param attrId
     * @return
     */
    @Override
    public BaseAttrInfo getAttrInfo(Long attrId) {
        BaseAttrInfo baseAttrInfo = baseAttrInfoMapper.selectById(attrId);
        // 查询到最新的平台属性值集合数据放入平台属性中！
        baseAttrInfo.setAttrValueList(getAttrValueList(attrId));
        return baseAttrInfo;
    }

    /**
     * 根据属性id获取属性值
     * @param attrId
     * @return
     */
    private List<BaseAttrValue> getAttrValueList(Long attrId) {
        // select * from baseAttrValue where attrId = ?
        LambdaQueryWrapper<BaseAttrValue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseAttrValue::getAttrId, attrId);
        List<BaseAttrValue> baseAttrValueList = baseAttrValueMapper.selectList(queryWrapper);
        return baseAttrValueList;
    }
}























